//
//  LoginViewController.m
//  Double
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "LoginViewController.h"
#import "FindPasswordViewController.h"
#import "RegisterViewController.h"
#import "MicroblogViewController.h"
#import "UserInformationDatabase.h"
#import "UserInformationModel.h"
#import "DB.h"
#import <ShareSDK/ShareSDK.h>
#import "MineViewController.h"
#import "UIView+textFieldShock.h"

#import "WeiboSDK.h"

@interface LoginViewController ()

@property (nonatomic, retain)UITextField * NumField;
@property (nonatomic, retain)UITextField * PasswordField;
@end

#define kAppKey         @"1837158850"
#define kRedirectURI    @"http://www.sharesdk.cn"

@implementation LoginViewController

- (void)dealloc
{
    [super dealloc];
}
- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createView];
    // Do any additional setup after loading the view.
}
#pragma mark 登陆界面
- (void)createView
{
    UIButton * microblogButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 30, [UIScreen mainScreen].bounds.size.width - 40, 50)];
    [microblogButton setTitle:@"微博账号登录" forState:UIControlStateNormal];
    [microblogButton setBackgroundColor:[UIColor colorWithRed:260/255.0 green:100/255.0 blue:100/255.0 alpha:1.0]];
    UIImageView * microblogImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 50, 50)];
    [microblogImage setImage:[UIImage imageNamed:@"1.png"]];
    [microblogButton addSubview:microblogImage];
    [microblogButton addTarget:self action:@selector(MicAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:microblogButton];
    [microblogButton release];
    
    UIButton * QQButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, 50)];
    [QQButton setTitle:@"QQ账号登录" forState:UIControlStateNormal];
    [QQButton setBackgroundColor:[UIColor colorWithRed:20 / 255.0 green:100 /255.0 blue:230 /255.0 alpha:1]];
    UIImageView * QQImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 50, 50)];
    [QQImage setImage:[UIImage imageNamed:@"2.png"]];
    [QQButton addTarget:self action:@selector(QQAction:) forControlEvents:UIControlEventTouchUpInside];
    [QQButton addSubview:QQImage];
    [self.view addSubview:QQButton];
    [QQButton release];
    
    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 170, [UIScreen mainScreen].bounds.size.width - 40, 30)];
    [label1 setText:@"看动漫账号登录"];
    [self.view addSubview:label1];
    [label1 release];
    
    self.NumField = [[UITextField alloc]initWithFrame:CGRectMake(20, 210, [UIScreen mainScreen].bounds.size.width - 40, 40)];
    [_NumField setBorderStyle:UITextBorderStyleRoundedRect];
    [_NumField setPlaceholder:@"请输入用户名"];
    [self.view addSubview:_NumField];
    [_NumField release];
    
    self.PasswordField = [[UITextField alloc]initWithFrame:CGRectMake(20, 260, [UIScreen mainScreen].bounds.size.width - 40, 40)];
    [_PasswordField setBorderStyle:UITextBorderStyleRoundedRect];
    [_PasswordField setPlaceholder:@"请输入密码"];
    UIButton * PasswordButton = [[UIButton alloc]initWithFrame:CGRectMake(_PasswordField.frame.size.width - 100, 0, 100, 40)];
    [PasswordButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [PasswordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [PasswordButton addTarget:self action:@selector(passwordAction:) forControlEvents:UIControlEventTouchUpInside];
    [_PasswordField addSubview:PasswordButton];
    [self.view addSubview:_PasswordField];
    [_PasswordField release];
    [PasswordButton release];
    
    UILabel * HintLable = [[UILabel alloc]initWithFrame:CGRectMake(20, 310, [UIScreen mainScreen].bounds.size.width - 40, 10)];
    [HintLable setText:@"已经绑定过手机的老用户,请点击忘记密码设定您的登录密码"];
    [HintLable setFont:[UIFont systemFontOfSize:10]];
    [self.view addSubview:HintLable];
    [HintLable release];
    
    UIButton * LoginButton = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 5, 330, [UIScreen mainScreen].bounds.size.width / 5, 30)];
    [LoginButton setTitle:@"登录" forState:UIControlStateNormal];
    [LoginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [LoginButton addTarget:self action:@selector(LoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:LoginButton];
    [LoginButton release];
    
    UIButton * RegisterButton = [[UIButton alloc]initWithFrame:CGRectMake(LoginButton.frame.size.width * 3, 330, LoginButton.frame.size.width, 30)];
    [RegisterButton setTitle:@"注册" forState:UIControlStateNormal];
    [RegisterButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [RegisterButton addTarget:self action:@selector(RegisterAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:RegisterButton];
    [RegisterButton release];
}
#pragma mark 微博登陆点击事件
- (void)MicAction:(id)sender
{
    MicroblogViewController * Mic = [[MicroblogViewController alloc]init];
    [self.navigationController pushViewController:Mic animated:YES];
    
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kRedirectURI;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];


}

#pragma mark QQ账号登陆点击事件
- (void)QQAction:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请等待..." delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    [alert release];
}
#pragma mark 忘记密码点击事件
- (void)passwordAction:(id)sender
{
    FindPasswordViewController * findPassword = [[FindPasswordViewController alloc]init];
    [self.navigationController pushViewController:findPassword animated:YES];
    [findPassword release];
}
#pragma mark 手机注册点击事件
- (void)RegisterAction:(id)sender
{
    RegisterViewController * Register = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:Register animated:YES];
    [Register release];
}
- (void)LoginAction:(id)sender
{
    NSArray *array = [UserInformationDatabase selectAll];
    for (UserInformationModel * models in array) {
        NSString *numfiler = models.Num;
        NSString *password = [NSString stringWithFormat:@"%ld", models.PassWord];
        if ([self.NumField.text isEqualToString:numfiler] && [self.PasswordField.text isEqualToString:password]) {
            NSLog(@"登录成功");
            UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登录成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alert1.tag = 100;
            [alert1 show];
            [alert1 release];
        }
        [self.NumField textFieldShock];
        [self.PasswordField textFieldShock];
        NSLog(@"登录失败");
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        MineViewController * mineView = [[MineViewController alloc]init];
        [self.navigationController pushViewController:mineView animated:YES];
        [mineView release];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
